package com.mohe.zhihui.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.mohe.zhihui.dao.IDao;
import com.mohe.zhihui.model.Message;
import com.mohe.zhihui.model.Volunteer;
import com.mohe.zhihui.service.interfaces.IVolunteer;
import com.mohe.zhihui.utils.Page;

public class VolunteerImpl implements IVolunteer {
	
	private IDao<Volunteer> volunteerDao;

	public IDao<Volunteer> getVolunteerDao() {
		return volunteerDao;
	}

	public void setVolunteerDao(IDao<Volunteer> volunteerDao) {
		this.volunteerDao = volunteerDao;
	}

	@Override
	public List<Volunteer> getVolunteerList(Volunteer volunteer, Page page) {
		StringBuffer where = new StringBuffer();
		where.append("from Volunteer where 1=1");
		List<Object> params = new ArrayList<Object>();
		if(!StringUtils.isBlank(volunteer.getName())){
			where.append(" and name like ?");
			params.add("%%"+volunteer.getName()+"%%");
		}
		where.append(" order by createDate desc");
		List<Volunteer> list = volunteerDao.query(where.toString(), (page.getCurrPage()-1)*page.getPageSize(), page.getPageSize(), params);
		if(list!=null && list.size()>0){
			return list;
		}
		return null;
	}

	@Override
	public Integer getCount(Volunteer volunteer) {
		StringBuffer where = new StringBuffer();
		where.append("from Volunteer where 1=1");
		List<Object> params = new ArrayList<Object>();
		if(!StringUtils.isBlank(volunteer.getName())){
			where.append(" and name like ?");
			params.add("%%" + volunteer.getName() + "%%");
		}
		return volunteerDao.getCount(where.toString(), params);
	}

	@Override
	public boolean saveVolunteer(Volunteer volunteer) {
		return volunteerDao.insert(volunteer);
	}

	@Override
	public Volunteer getVolunteerById(Long timeKey) {
		Volunteer volunteer = volunteerDao.getByKey(timeKey);
		if(volunteer != null){
			return volunteer;
		}
		return null;
	}

	@Override
	public boolean updateVolunteer(Volunteer volunteer) {
		return volunteerDao.update(volunteer);
	}

	@Override
	public boolean delVolunteer(Volunteer volunteer) {
		return volunteerDao.delete(volunteer.getTimeKey());
	}

	@Override
	public List<Volunteer> getAllVolunteer() {
		StringBuffer where = new StringBuffer();
		where.append("where timeKey > -1 order by createDate desc");
		return volunteerDao.query(where.toString(), null);
	}

	@Override
	public Volunteer getVolunteerByPhone(String phone) {
		StringBuffer where = new StringBuffer();
		where.append("from Volunteer where 1=1");
		List<Object> params = new ArrayList<Object>();
		if(!StringUtils.isBlank(phone)){
			where.append(" and phone=?");
			params.add(phone);
		}
		List<Volunteer> list = volunteerDao.query(where.toString(), params);
		if(list != null && list.size() > 0){
			return list.get(0);
		}
		return null;
	}

}
